#include "mat_game.h"
#include "mat_tutorial.h"
#include "mat_gamerule.h"
#include "mat_animation_mgr.h"
#include "mat_gemcollect.h"
#include "mat_logicboard.h"
#include "mat_scoring.h"
#include "mat_sfx_manager.h"
#include "mat_sounds.h"
#include "mat_ui_animator.h"
#include "mat_ui_popupanimator.h"
#include "mat_game_editor.h"
#include "mat_level_manager.h"
#include "mat_char_animation.h"
#include "mat_ui_popupanimator.h"
#include "mat_gem.h"
#include "gen_game_state.h"

#include "ui/ui_flexeffect.h"
#include "admanager/admanager.h"
#include "application/game_rms.h"
#include "application/game_application.h"
#include "font/font_manager.h"
#include "graphics/graphics_util.h"
#include "math/intersection.h"
#include "gameflow/ui_popup.h"


math::Vec2			MATGame::m_BoardPos[MAX_COUNT_Y][MAX_COUNT_X];
ui::UISprite		MATGame::m_SprGem[GEM_UNIQUE_COUNT];
ui::UISprite		MATGame::m_SprBomb[GEM_UNIQUE_COUNT];
ui::UISprite		MATGame::m_SprGemGlow[GEM_UNIQUE_COUNT];
ui::UISprite		MATGame::m_SprBombGlow[GEM_UNIQUE_COUNT];

math::Vec2			MATGame::m_SizeGemStatic;

bool	RenderOverUICallBack()
{
	GENGameState::GetGame()->GetGameMatch5()->RenderOverUI();
	return true;
}

MATGame::MATGame() : m_pEditor(NULL),
					 m_PathMoveIndex(0),
					 m_pUIPause(NULL),
					 m_NotValideMoveShowDelay(0),
					 m_pUISetting(NULL)
{

	m_Scale = (float)GraphicsUtil::WIDTH / (float)TEXTURE_WIDTH;

	m_BoardFrame.Create("in_game_frame");
	m_BoardFrame.m_Color = ColorfRGBA(1.0f,1.0f,1.0f,0.94f);
	const math::Vec2 TempSize = m_BoardFrame.m_Size;
	m_BoardFrame.m_Size *= (m_Scale);	
	m_BoardFrame.m_Pos = math::Vec2(0.5,0.45f);

	float CAP_HEIGHT = 0.63f;
	bool IsIphone = false;

	if (    ( GraphicsUtil::WIDTH == 640.0f && GraphicsUtil::HEIGHT == 960.0f) ||
			( GraphicsUtil::WIDTH == 320.0f && GraphicsUtil::HEIGHT == 480.0f) ||
            ( GraphicsUtil::WIDTH == 640.0f && GraphicsUtil::HEIGHT == 1136.0f)
        
			)
	{
		CAP_HEIGHT = 0.7f;			// iPhone4
		IsIphone = true;
	}

	if(m_BoardFrame.m_Size.y > CAP_HEIGHT)
	{
		const math::Vec2 TempSize2 = TempSize * ( 1.0f / TempSize.y);	
		m_BoardFrame.m_Size = TempSize2 * ( CAP_HEIGHT / 1.0f );
	}

	m_Scale = m_BoardFrame.m_Size.x / TempSize.x;

	char Buffer[256];
	for(int i = 0; i < GEM_UNIQUE_COUNT; i++)
	{
		if(IsIphone)
			sprintf(Buffer,"i4_gem_%i", + i + 1);			
		else		
			sprintf(Buffer,"gem_%i", + i + 1);

		m_SprGem[i].Create(Buffer);

		if(IsIphone)
			sprintf(Buffer,"i4_glow_%i", + i + 1);	
		else		
			sprintf(Buffer,"glow_%i", + i + 1);
		
		m_SprGemGlow[i].Create(Buffer);

		sprintf(Buffer,"bomb_%i", + i + 1);
		m_SprBomb[i].Create(Buffer);		

		sprintf(Buffer,"bomb_select_%i", + i + 1);
		m_SprBombGlow[i].Create(Buffer);

		
	}

	for(int i = 0; i < RENDER_BUFFER; i++)
	{
		m_RenderList[i].m_pSprite = NULL;
	}


	if(IsIphone)
	{		
		m_SizeGem      = m_SprGem[0].m_Size * ((float)GraphicsUtil::WIDTH / 640.0f);
	}
	else
	{
		m_SizeGem      = m_SprGem[0].m_Size * m_Scale;
	}
	m_SizeGemStatic = m_SizeGem;	
	m_SizeGemSmall = m_SizeGem * 0.5f;
	

	m_SprBoard_BG.Create("light_tile");
	m_SprBoard_BG.m_Size *= m_Scale;	

	m_SprTimeBonus.Create("bonus_time");
	m_SprTimeBonus.m_Size *= m_Scale;

	m_SprBombFire.Create("bomb_fire");
	m_SprBombFire.m_Size *= m_Scale;
	
	m_SprNotValidMove.Create("cross");
	m_SprNotValidMove.m_Size *= m_Scale;

	MATGameRule::Instance().Start();
	
	for(int i = 1; i < TILE_COUNT; i ++)
	{
		sprintf(Buffer, "tile_%i", i);
		m_SprTiles[i].Create(Buffer);
		m_SprTiles[i].m_Size *= m_Scale;
	}

	m_BoardStart = m_BoardFrame.m_Pos;
	m_BoardStart -= (m_SprBoard_BG.m_Size * 4.0f);
	
	float PosX = m_BoardStart.x;
	float PosY = m_BoardStart.y;
	for(int y = 0; y< MATGameRule::Instance().GetBoardCountY(); y++)
	{
		PosX = m_BoardStart.x;
		for(int x = 0; x < MATGameRule::Instance().GetBoardCountX(); x++)
		{			
			m_BoardPos[y][x] = math::Vec2(PosX,PosY);
			PosX +=  m_SprBoard_BG.m_Size.x ;			
		}
		PosY += m_SprBoard_BG.m_Size.y;
	}

	m_BombTurn.Load("ui_img/match5_number.lua");

	//MATUIPopupAnimator* pPopupAnimator = MATUIPopupAnimatorMgr::Instance().GetPopup(MATUIPopupAnimatorMgr::POPUP_NAMES_PAUSEMENU);
	//m_pUIPause = pPopupAnimator->GetPopup();

	//Load("levels/level_1.lua");

	const float RemaninHeight = 1.0f - m_BoardFrame.m_Size.y;
	ui::UIWidget* pGameInfoFrame = ui::UIManager::Instance().GetWidget("button_frame");
	if(pGameInfoFrame)
	{
		const float TempPos =    m_BoardFrame.m_Size.y + ( RemaninHeight * 0.5f) - ( pGameInfoFrame->GetSize().y * 0.5f);
		//pGameInfoFrame->SetPosition(math::Vec2( pGameInfoFrame->GetPosition().x, TempPos ));

		ui::UIWidget* pChestPos = ui::UIManager::Instance().GetWidget("pos_chest");
		if(pChestPos)
		{
			const float TempPos2 =    pGameInfoFrame->GetPosition().y + pGameInfoFrame->GetSize().y - (pChestPos->GetSize().y * 0.45f);
			//pChestPos->SetPosition( math::Vec2(pChestPos->GetPosition().x, TempPos2));	
			
		}
	}

	ui::UIFlexEffect* pUIFlexEffect = (ui::UIFlexEffect*)ui::UIManager::Instance().GetWidget("flexeffect");
	if(pUIFlexEffect)
			pUIFlexEffect->SetRenderFunc((ui::UIFlexEffect::FLEXEFFECT_RENDERFUNCTION)RenderOverUICallBack);
}

MATGame::~MATGame()
{
	UTIL_SAFE_DELETE(m_pEditor);
}

void		MATGame::ChestPart2D::Load(const std::string& File, float Scale)
{
	m_PartChestTreasure.Load(File);
	m_PartChestTreasure.SetPos(m_SpawnPos/Scale);
}


void		MATGame::RenderTile(bool IsRenderOverGem)
{
	TILE_TYPE TileType;
	ui::UISprite* pTempSpr = NULL;

	for(int y = 0; y< MATGameRule::Instance().GetBoardCountY(); y++)
	{
		
		for(int x = 0; x < MATGameRule::Instance().GetBoardCountX(); x++)
		{
			TileType = MATLogicBoard::Instance().GetTileAt(y,x);

			switch(TileType)
			{
			case TILE_NORMAL:
				/*
				if(k % 2 == 0)
					pTempSpr = &m_SprBoard_BG;
				else
					pTempSpr = &m_SprBoard_BG2;

				pTempSpr->m_Pos = math::Vec2(PosX,PosY);
				pTempSpr->RenderRotated();
				*/

				break;

			case TILE_HARD:

				if(IsRenderOverGem == true)
				{}
				else
				{
					pTempSpr = &m_SprTiles[TILE_HARD];
					pTempSpr->m_Pos = m_BoardPos[y][x] + m_AnimateBoard.m_Offset;
					pTempSpr->RenderRotated();
				}

				break;

			case TILE_UNMOVEABLEGEM:

				if(IsRenderOverGem == false)
				{
					
				}
				else
				{
					pTempSpr = &m_SprTiles[TILE_UNMOVEABLEGEM];
					pTempSpr->m_Pos = m_BoardPos[y][x] + + m_AnimateBoard.m_Offset;
					pTempSpr->RenderRotated();
					
				}						
				break;


				
			case TILE_UNMOVEABLEGEM2:

				if(IsRenderOverGem == false)
				{
					pTempSpr = &m_SprTiles[TILE_HARD];
				}
				else
				{
					pTempSpr = &m_SprTiles[TILE_UNMOVEABLEGEM2];
					
				}

				if(pTempSpr)
				{
					pTempSpr->m_Pos = m_BoardPos[y][x] + + m_AnimateBoard.m_Offset;
					pTempSpr->RenderRotated();
				}
				
				break;

			case TILE_UNREACHABLE:
				if(IsRenderOverGem == true)
				{}
				else
				{
					pTempSpr = &m_SprTiles[TILE_UNREACHABLE];
					pTempSpr->m_Pos = m_BoardPos[y][x] + + m_AnimateBoard.m_Offset;
					pTempSpr->RenderRotated();
				}
				
				break;

			case TILE_UNREACHABLE2:
				if(IsRenderOverGem == true)
				{}
				else
				{
					pTempSpr = &m_SprTiles[TILE_UNREACHABLE2];
					pTempSpr->m_Pos = m_BoardPos[y][x] + + m_AnimateBoard.m_Offset;
					pTempSpr->RenderRotated();
				}
				
				break;
			}
			
			
		}
		
	}
}

void		MATGame::Render()
{
	//return;
	// -------- draw board ----------------------------------

	m_BackGround.RenderRotated();
	m_BoardFrame.m_Pos = math::Vec2(0.5f,0.45f) + m_AnimateBoard.m_Offset;
	m_BoardFrame.RenderRotated();
	
	RenderTile(false);

	//----------- draw gems ----------------------------------
	const MATPathFinding* pPathFinding = MATLogicBoard::Instance().GetPathFinding();	

	for(int i = 0; i < RENDER_BUFFER; i++)
	{
		if(m_RenderList[i].m_pSprite == NULL)
			break;

		if(MATGameRule::Instance().IsTutorialActive() && m_RenderList[i].m_IsAfterTutorial)
		{

		}
		else
		{
			m_RenderList[i].m_pSprite->m_Pos   = m_AnimateBoard.m_Offset + m_RenderList[i].m_Pos;
			m_RenderList[i].m_pSprite->m_Size  = m_RenderList[i].m_Size;
			m_RenderList[i].m_pSprite->m_Color = m_RenderList[i].m_Color;
			m_RenderList[i].m_pSprite->m_Rotation = m_RenderList[i].m_Rotation;
			m_RenderList[i].m_pSprite->RenderRotated();
		}
	}

	m_Sparkle.Render(m_AnimateBoard.m_Offset);
	m_Arrow.Render(m_PathMoveIndex, m_Scale);

	RenderTile(true);

	// Render BombTurns
	MATGem* pGem = NULL;		
	for(int y = 0; y< MATGameRule::Instance().GetBoardCountY(); y++)
	{		
		for(int x = 0; x < MATGameRule::Instance().GetBoardCountX(); x++)
		{
			pGem = MATLogicBoard::Instance().GetContentAt(y,x);
			if(pGem && pGem->m_IsBomb && pGem->m_IsSmall == false)
			{				
				if(pGem->m_BombTurnToExplode > 0)					
					m_BombTurn.Draw(pGem->m_BombTurnToExplode,
									GetBoardPos(y,x)- math::Vec2( GetGemSize().x * 0.1f, 0) + m_AnimateBoard.m_Offset,
									1.0f);
				else
					m_BombTurn.Draw(0,
									GetBoardPos(y,x)- math::Vec2( GetGemSize().x * 0.1f, 0) + m_AnimateBoard.m_Offset,
									1.0f);
			}
		}
	}

	
	// ------------ Tutorial ----------------------------
	if(MATGameRule::Instance().IsTutorialActive())
	{
		MATGameRule::Instance().GetTutorial()->Render(m_Scale);

		for(int i = 0; i < RENDER_BUFFER; i++)
		{
			if(m_RenderList[i].m_pSprite == NULL)
				break;

			if( m_RenderList[i].m_IsAfterTutorial)
			{			
				m_RenderList[i].m_pSprite->m_Pos   = m_RenderList[i].m_Pos;
				m_RenderList[i].m_pSprite->m_Size  = m_RenderList[i].m_Size;
				m_RenderList[i].m_pSprite->m_Color = m_RenderList[i].m_Color;
				m_RenderList[i].m_pSprite->m_Rotation = m_RenderList[i].m_Rotation;
				m_RenderList[i].m_pSprite->RenderRotated();
			}
		}
	}

	MATSFXManager::Instance().Render(math::Vec2(0,0),m_Scale);

	if(m_pEditor)
		m_pEditor->Render(math::Vec2(0,0),m_Scale);

	MATLevelManager::Instance().Render(m_Scale);	

	//m_SprBomb.m_Color = ColorRGBA(16,128,218,255); 
	if(m_NotValideMoveShowDelay > 0)
	{
		m_SprNotValidMove.RenderRotated();
	}
	
}


void		MATGame::RenderOverUI()
{
	MATScoring::Instance().Render(m_Scale);
	MATCharAnimation::Instance().Render();
	MATGemCollect::Instance().Render();
}

void		MATGame::PostRender()
{
    MATSFXManager::Instance().PostRender(math::Vec2(0,0),m_Scale);

	
}

void		MATGame::RenderDebug()
{
	FontManager::Instance().Print(math::Vec2(0.5f,0.5f),
								"%f,%f", 
								input::Mouse::Instance().GetPos().x,
								input::Mouse::Instance().GetPos().y
								); 
}

void		MATGame::ChestPart2D::Tick(float dt, float Scale)
{
	m_ReSpawnPart -= dt;
	if(m_ReSpawnPart <= 0)
	{
		m_ReSpawnPart = 0.1f;
		m_PartChestTreasure.TriggerParticleSet(1,m_SpawnPos / Scale );
		
	}
	m_PartChestTreasure.Tick(dt);
	
}

void		MATGame::Tick(float dt)
{
	
	MATUIAnimator::Instance().Tick(dt);
	MATUIPopupAnimatorMgr::Instance().Tick(dt);

	if(m_pUIPause)
	{
		if(m_pUIPause->IsActive())
			return;
	}
	else
	{
		MATUIPopupAnimator* pPopupAnimator = MATUIPopupAnimatorMgr::Instance().GetPopup(MATUIPopupAnimatorMgr::POPUP_NAMES_PAUSEMENU);
		m_pUIPause = pPopupAnimator->GetPopup();		
	}

	if(m_pUISetting)
	{
		if(m_pUISetting->IsActive())
			return;
	}
	else
	{
		MATUIPopupAnimator* pPopupAnimator = MATUIPopupAnimatorMgr::Instance().GetPopup(MATUIPopupAnimatorMgr::POPUP_NAMES_SETTING);
		m_pUISetting = pPopupAnimator->GetPopup();		
	}

	
	m_AnimateBoard.Tick(dt);
	MATGameRule::Instance().Tick(dt);
	m_SprBombFire.Tick(dt);
	m_BoardFrame.Tick(dt);
	m_BackGround.Tick(dt);
	MATCharAnimation::Instance().Tick(dt);
	MATLogicBoard::Instance().Tick(dt);
	m_Sparkle.Tick(dt);
	MATAnimationManager::Instance().Tick(dt);	
	MATGemCollect::Instance().Tick(dt);
	MATScoring::Instance().Tick(dt);
	MATSFXManager::Instance().Tick(dt);
	MATSounds::Instance().Tick(dt);
	MATLevelManager::Instance().Tick(dt);

	const MATPathFinding* pPathFinding = MATLogicBoard::Instance().GetPathFinding();
	if(pPathFinding->IsFound() == false && MATAnimationManager::Instance().IsActiveAnimation(MATAnimation::ANI_TYPE_MOVING) == false)
	{
		m_PathMoveIndex = 0;
		m_Arrow.Clear();
	}

	if(pPathFinding->IsDirty())
		m_Arrow.Build(pPathFinding->GetPath());
	
	if(MATGameRule::Instance().IsTutorialActive())
		MATGameRule::Instance().GetTutorial()->Tick(dt);

	BuildRenderList();
	

	if(m_pEditor)
		m_pEditor->Tick(dt);

	if(m_NotValideMoveShowDelay > 0)
	{
		m_NotValideMoveShowDelay -= dt;
	}
}




ui::UISprite*		MATGame::GetSpriteByGem(MATGem*  BC)
{
	if(!BC)
		return NULL;

	if(BC->m_Type == 0)
		return NULL;

	if(BC->m_Type > GEM_UNIQUE_COUNT)
		return NULL;

	if(BC->m_IsBomb)
	{
		if(BC->m_IsGlow)
		{
			return &m_SprBombGlow[BC->m_Type - 1];
			
		}
		else 
		{
			return &m_SprBomb[BC->m_Type - 1];
		}
	}
	else
	{

		if(BC->m_IsGlow)
		{
			return &m_SprGemGlow[BC->m_Type - 1];
			
		}
		else 
		{
			return &m_SprGem[BC->m_Type - 1];
		}
	}
	return NULL;
}


void		MATGame::KeyPressed(input::Keyboard::KEY* pKey)
{
	if(m_pEditor)
		m_pEditor->KeyPressed(pKey);

	switch(pKey->KeyID)
	{
		case KEY_F1:
			if (m_pEditor)
			{
				UTIL_SAFE_DELETE(m_pEditor);			
			} 
			else
			{
				m_pEditor = new MATGameEditor(this);			
			}
		break;

		case KEY_RETURN:
			MATGameRule::Instance().NextTurn();
		break;
		

		default: 
			MATCharAnimation::Instance().KeyPressed(pKey);
			break;
	}
}

void		MATGame::MouseButtonDown(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos)
{
	if(m_pEditor)
	{
		m_pEditor->MouseButtonDown(pKey, Pos);
		return;
	}

	MATUIPopupAnimatorMgr::Instance().OnMouseButtonDown(Pos);
	if(gameflow::UIPopupManager::Instance().GetActivePopup())
		return;

	

	math::Vec2 BPos;
	for(int y = 0; y< MATGameRule::Instance().GetBoardCountY(); y++)
	{		
		for(int x = 0; x < MATGameRule::Instance().GetBoardCountX(); x++)
		{
			BPos = m_BoardPos[y][x];
			if( math::Intersection::PointInsideRect2DFast(Pos, BPos, m_SizeGem, true)
				)
			{
				if( MATGameRule::Instance().IsTutorialActive())
				{
					MATGameRule::Instance().MouseButtonDown(Pos,y,x);
					return;
				}

				MATGem* BC = MATLogicBoard::Instance().MouseButtonDown(Pos, y, x);				
				return;
			}
            		
		}
	}	
}

void		MATGame::GetBoardXY(const math::Vec2& Pos, int& X, int& Y)
{
	math::Vec2 BPos;
	for(int y = 0; y< MATGameRule::Instance().GetBoardCountY(); y++)
	{		
		for(int x = 0; x < MATGameRule::Instance().GetBoardCountX(); x++)
		{
			BPos = m_BoardPos[y][x];
			if( math::Intersection::PointInsideRect2DFast(Pos, BPos, m_SizeGem, true)
				)
			{
				Y = y;
				X = x;
				return;
			}
            		
		}
	}	

	Y = -1;
	X = -1;
}

void		MATGame::MouseButtonReleased(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos)
{
	bool ToShowNotValidMove = false;
	if(gameflow::UIPopupManager::Instance().GetActivePopup())
		return;

	if(m_pEditor)
	{
		m_pEditor->MouseButtonReleased(pKey, Pos);
		return;
	}

	

	math::Vec2 BPos;
	for(int y = 0; y< MATGameRule::Instance().GetBoardCountY(); y++)
	{		
		for(int x = 0; x < MATGameRule::Instance().GetBoardCountX(); x++)
		{
			BPos = m_BoardPos[y][x];
			if( math::Intersection::PointInsideRect2DFast(Pos, BPos, m_SizeGem, true)
				)
			{
				if( MATGameRule::Instance().IsTutorialActive())
				{
					MATGameRule::Instance().MouseButtonReleased(Pos,y,x);
					return;
				}

				
				MATLogicBoard::Instance().MouseButtonReleased(Pos, y, x, ToShowNotValidMove);
				if(ToShowNotValidMove)
					ShowNotValidMove(y,x);
				return;
			}
            		
		}
	}
	MATLogicBoard::Instance().MouseButtonReleased(Pos, -1, -1, ToShowNotValidMove);
	

}

void		MATGame::MouseButtonPressed(input::Mouse::BUTTON_STATE* pKey, const math::Vec2& Pos)
{
	if(m_pEditor)
	{
		m_pEditor->MouseButtonPressed(pKey, Pos);
		return;
	}
}

void		MATGame::MouseMove(const math::Vec2& From, const math::Vec2& To)
{
	if(m_pEditor)
	{
		m_pEditor->MouseMove(From,To);
		return;
	}
}

void		MATGame::MouseWheelMove(int WheelInfo, const math::Vec2& Pos)
{
	if(m_pEditor)
	{
		m_pEditor->MouseWheelMove(WheelInfo, Pos);
		return;
	}	
}


void		MATGame::BuildRenderList()
{
	for(int i = 0; i < RENDER_BUFFER; i++)
	{
		m_RenderList[i].m_pSprite = NULL;
	}


	int Index = 0;
	MATGem* pLogicType;
	math::Vec2 Pos;
	math::Vec2 Size;
	COLOR c;
	float Rotation;
	MATAnimation* pAni;
	bool IsSelected;

	for(int y = 0; y< MATGameRule::Instance().GetBoardCountY(); y++)
	{		
		for(int x = 0; x < MATGameRule::Instance().GetBoardCountX(); x++)
		{
			pLogicType = MATLogicBoard::Instance().GetContentAt(y,x);
			pAni = MATAnimationManager::Instance().GetActiveAnimAt(y,x);

			if(!pLogicType && (pAni && !pAni->GetGemType() ))
				continue;

			if( (pLogicType && pLogicType->m_Type == 0) && !pAni)
				continue;

			IsSelected = MATLogicBoard::Instance().IsSelected(y,x);
			Pos = m_BoardPos[y][x];
			
			if(pAni && !pAni->IsFinish())
			{
				pLogicType = pAni->GetGemType();						
				
				c = ColorfRGBA(1.0f,1.0f,1.0f,1.0f);
				Rotation = 0;
				
				if(pLogicType->m_IsGlow )
					Size = m_SizeGem;
				else if(pLogicType->m_IsSmall )
					Size = m_SizeGemSmall;
				else
					Size = m_SizeGem;
				
				pAni->GetInfos(Pos, Size,c,Rotation);
				AddToRenderList(Index, GetSpriteByGem(pLogicType),  pLogicType, Pos, Size, c, Rotation);
				if(pAni->GetType() == MATAnimation::ANI_TYPE_MOVING)
				{
					m_PathMoveIndex = ((MATAniMoving*)pAni)->GetMoveIndex();
				}
					

				//if(pLogicType->m_IsGlow )
				{					
				//	Size = m_SizeGem;
				//	AddToRenderList(Index, GetSpriteByGem(pLogicType), Pos, Size, pLogicType->m_IsTimeBonus);
					
				}
			}
			else if(IsSelected)
			{
				AddToRenderList(Index, GetSpriteByGem(pLogicType) , pLogicType , Pos, m_SizeGem);
			}
			else if(pLogicType)
			{
				if(pLogicType->m_IsSmall )
					Size = m_SizeGemSmall;
				else
					Size = m_SizeGem;

				AddToRenderList(Index, GetSpriteByGem(pLogicType) , pLogicType , Pos, Size);
			}
			
		
		}
	}
}

void		MATGame::AddToRenderList(	int& Index, 												
										ui::UISprite* pSprite, 
										MATGem*	pGem,
										const math::Vec2 Pos, 
										const math::Vec2 Size, 												
										COLOR c, 
										float Rotation)
{
	if(pSprite == NULL)
		return;

	if( Index > RENDER_BUFFER)
		return;

	if(!pGem)
		return;

	m_RenderList[Index].m_pSprite = pSprite;
	m_RenderList[Index].m_Pos = Pos;
	m_RenderList[Index].m_Size = Size;
	m_RenderList[Index].m_Color = c;
	m_RenderList[Index].m_Rotation = Rotation;
	if(MATGameRule::Instance().IsTutorialActive() && pGem->m_IsGlow )
	{
		m_RenderList[Index].m_IsAfterTutorial = true;
	}
	else
	{
		m_RenderList[Index].m_IsAfterTutorial = false;
	}
	Index++;

	if(pGem->m_IsTimeBonus)
	{
		m_RenderList[Index].m_pSprite = &m_SprTimeBonus;
		m_RenderList[Index].m_Pos = Pos;
		m_RenderList[Index].m_Size = Size;
		m_RenderList[Index].m_Color = c;
		m_RenderList[Index].m_Rotation = Rotation;
		Index++;
	}

	if(pGem->m_IsBomb && pGem->m_BombTurnToExplode <= 10)
	{
		m_RenderList[Index].m_pSprite = &m_SprBombFire;
		m_RenderList[Index].m_Pos = Pos;
		m_RenderList[Index].m_Size = Size;
		m_RenderList[Index].m_Color = c;
		m_RenderList[Index].m_Rotation = Rotation;
		Index++;
	}

	
}

void		MATGame::ClearRenderList()
{
	for(int i = 0; i < RENDER_BUFFER; i++)
	{
		m_RenderList[i].m_pSprite = NULL;
	}
	m_PathMoveIndex = 0;
	m_Arrow.Clear();
}

bool		MATGame::Clear()
{
	return true;
}

bool		MATGame::Load(const std::string& File)
{

	m_Sparkle.ForceFinish();
	MATLogicBoard::Instance().LoadLevel(File);
	BuildRenderList();	
	return true;
	
}

bool	MATGame::Save(const std::string& File)
{
	MATLogicBoard::Instance().Save(File);
	return true;
}




ui::UISprite*		MATGame::GetTileSprite(int Type)
{
	return &m_SprTiles[Type];
}

void	MATGame::ShowNotValidMove(int Y, int X)
{
	m_NotValideMoveShowDelay = 0.75f;
	m_SprNotValidMove.m_Pos = GetBoardPos(Y,X);
	MATSounds::Instance().PlaySoundSFX(MATSounds::SFX_NOTVALIDMOVE);
}


void	MATGame::SetBackGround(const std::string& FileName)
{
	if( FileName.compare(m_BackGround.GetFilename()) == 0)
		return;

	m_BackGround.Create(FileName);
	m_BackGround.m_Pos = math::Vec2(0.5f,0.5f);
	m_BackGround.m_Size *= (1.0f / m_BackGround.m_Size.y);
    
	
	
}

void	MATGame::SetBackGround()
{
	MATLevelManager::Instance().SetGameBackground();
}

void	MATGame::AnimateHideBoard(float Delay)
{
	m_AnimateBoard.Hide(Delay);
}

void	MATGame::AnimateShowBoard(float Delay)
{
	m_AnimateBoard.Show(Delay);
}
